<?php

namespace app\admin\controller;

use app\admin\controller\Base;
use app\admin\model\Category;
use think\Request;
use think\Db;

class Cate extends Base
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $data = [];
        $rel = Db::name('category')->paginate(1000,false);
        $data =  getTree($rel->items());


        return view('category/index', ['data'=>$data]);
    }





    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {

        $info = Category::all();
        $arr =[];
        foreach ($info as $val){
            $arr[] = $val->getData();
        }
        $data = getTree($arr);
        return view('category/add',['data'=>$data]);
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $info = input('post.');


        $result = Category::create($info)->getData();
        $data = [];
        if ($result['id']>0){
            $data['status'] = true;
            $data['message'] = '添加成功';
        }else{
            $data['status'] = false;
            $data['message'] = '添加失败';
        }

        return json($data);
    }


    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //接收表单数据
       $info = input('put.');

       //查询该分类
       $category = Category::get($id);
       //得到分类信息并添加count字段
       $data = getTree(Db::name('category')->select());
       $cate = [];
       foreach ($data as $val){
           if ($val['id']==$info['pid']){
               $cate = $val;
           }
       }
       //判断是否是顶级分类
       if($info['pid']=='first'){
           $category->cname = $info['cname'];
           $category->pid = 0;
           $result = $category->save();
           if ($result>0){
               $data['status'] = true;
               $data['message'] = '修改成功';
           }else{
               $data['status'] = false;
               $data['message'] = '没有做出修改';
           }
           return json($data);
       }
        $data = [];
       if ($info['count'] > $cate['count']){
           $category->cname = $info['cname'];
           $category->pid = $info['pid'];
           $result = $category->save();
           if ($result>0){
               $data['status'] = true;
               $data['message'] = '添加成功';
           }else{
               $data['status'] = false;
               $data['message'] = '添加失败';
           }
       }else{
           $data['status'] = false;
           $data['message'] = '修改失败,子类不能移动到父类和同级类中';
       }

       return json($data);
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $category =  Category::get($id);
        $data = getTree(Db::name('category')->select());
        $flag = true;
        foreach ($data as $val){
            if ($id == $val['pid']){
                $flag = false;
                continue;
            }
        }

        if($flag){
            $result = $category->delete();
            $data = [];
            if ($result>0){
                $data['status'] = true;
                $data['message'] = '删除成功';
            }else{
                $data['status'] = false;
                $data['message'] = '删除失败';
            }
        }else{
            $data['status'] = false;
            $data['message'] = '删除失败，有子分类不能删除';
        }
        return json($data);
    }
}
